package lslnx.controller;

import com.alibaba.fastjson.JSON;
import lslnx.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * Created by lslnx on 2017/4/25.
 */
@Controller
public class AccountController {


    @RequestMapping("/toLogin")
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request,Map<String,Object> map){
        try {
            String userName = request.getParameter("userName");
            String password = request.getParameter("passWord");
            UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            map.put("msg",e.getMessage());
            throw e;
        }catch (IncorrectCredentialsException e){
            map.put("msg",e.getMessage());
            e.printStackTrace();
            System.out.print(e.getMessage());
            throw e;
        }
        return "success";
    }

}
